package com.hb.trade.server.project.notice.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.enums.NoticeNatureEnum;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.dto.NoticeListDTO;
import com.hb.core.pojo.project.notice.dto.NoticePortalDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.core.pojo.project.notice.entity.NoticeBidSection;
import com.hb.core.pojo.project.notice.mapper.NoticeMapper;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.notice.service.NoticeBidSectionService;
import com.hb.trade.server.project.notice.service.NoticeService;
import com.hb.trade.server.project.notice.service.TenderInviteEnterpriseService;
import com.hb.trade.server.project.proj.service.ProjectService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import org.apache.commons.collections4.CollectionUtils;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 公告信息表 服务实现类
 * </p>
 *
 * @author zhaojk
 * @since 2021-08-30
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    protected final BidSectionService bidSectionService;
    protected final ProjectService projectService;
    protected final NoticeBidSectionService noticeBidSectionService;
    protected final TenderInviteEnterpriseService tenderInviteEnterpriseService;

    public NoticeServiceImpl(BidSectionService bidSectionService,
                             ProjectService projectService,
                             NoticeBidSectionService noticeBidSectionService,
                             TenderInviteEnterpriseService tenderInviteEnterpriseService) {
        this.bidSectionService = bidSectionService;
        this.projectService = projectService;
        this.noticeBidSectionService = noticeBidSectionService;
        this.tenderInviteEnterpriseService = tenderInviteEnterpriseService;
    }

    /**
     * 根据公告id查询公告信息某些字段
     *
     * @param id
     * @param columns
     * @return
     */
    private NoticeDTO findNoticeById(Long id, String... columns) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper();
        queryWrapper.select(columns).eq("id", id);
        return this.getOne(queryWrapper).to(NoticeDTO.class);
    }

    /**
     * 根据公告id查询公告下发布的标段信息
     *
     * @param noticeId
     * @return
     */
    private JSONArray findNoticeSectionListById(Long noticeId) {
        JSONArray purchaseFileBidArray = JSONUtil.createArray();//采购文件信息下的标段
        List<JSONObject> sectionPriceList = bidSectionService.findSectionPriceByNoticeId(noticeId);
        if (CollectionUtils.isNotEmpty(sectionPriceList)) {
            for (JSONObject sectionJSON : sectionPriceList) {

                String sectionIdListString = sectionJSON.get("sectionIdList", String.class);
                String sectionNameListListString = sectionJSON.get("sectionNameList", String.class);
                String[] sectionIdArr = sectionIdListString.split(",");
                String[] sectionNameArr = sectionNameListListString.split(",");
                JSONArray sectionIdJsonArray = JSONUtil.createArray();
                int length = sectionIdArr.length;
                for (int i = 0; i < length; i++) {
                    JSONObject sectionIdAndSectionNameJson = JSONUtil.createObj();
                    sectionIdAndSectionNameJson.putOpt("id", sectionIdArr[i]);
                    sectionIdAndSectionNameJson.putOpt("sectionName", sectionNameArr[i]);
                    sectionIdJsonArray.add(sectionIdAndSectionNameJson);
                }
                sectionJSON.remove("sectionIdList");
                sectionJSON.remove("sectionNameList");
                sectionJSON.putOpt("sectionList", sectionIdJsonArray);
                purchaseFileBidArray.add(sectionJSON);
            }
        }
        return purchaseFileBidArray;
    }

    private JSONObject checkAndFile(Long sectionId) {
        JSONObject jsonObject = JSONUtil.createObj();
        BidSection bidSection = bidSectionService.getById(sectionId);
        if (bidSection == null) {
            jsonObject.putOpt("msg", "标段信息不存在,请重新进入");
            return jsonObject;
        }
        NoticeBidSection noticeBidSection = noticeBidSectionService.findLastNoticeBidSection(sectionId);//公告标段中间表
        if (noticeBidSection == null) {
            noticeBidSection = new NoticeBidSection();
        }
        jsonObject.putOpt("bidSection", bidSection);
        jsonObject.putOpt("noticeBidSection", noticeBidSection);
        return jsonObject;
    }

    @Override
    public List<Notice> findNoticeBySectionId(Long sectionId, Integer noticeNature) {
        return findNoticeBySectionIds(CollUtil.list(false, sectionId), noticeNature);
    }

    /**
     * 查询指定标段的公告性质
     *
     * @param sectionIds
     * @param noticeNature
     * @return
     */
    @Override
    public List<Notice> findNoticeBySectionIds(List<Long> sectionIds, Integer noticeNature) {
        if (CollUtil.isEmpty(sectionIds)) {
            return CollUtil.list(false);
        }
        return baseMapper.findNoticeBySectionIds(sectionIds, noticeNature);
    }

    @Override
    public Page<NoticeDTO> listNoticeBySection(NoticeDTO notice) {
        Page<NoticeDTO> pages = notice.pages();
        List<NoticeDTO> noticeVos = baseMapper.listNoticeBySection(pages, notice);
        for (NoticeDTO noticeVo : noticeVos) {
            noticeVo.setNoticeNatureName(Enums.nameOf(NoticeNatureEnum.class, noticeVo.getNoticeNature()));
            noticeVo.setDeployStatusName(Enums.nameOf(DeployStatusEnum.class, noticeVo.getDeployStatus()));
        }
        pages.setRecords(noticeVos);
        return pages;
    }

    /**
     * 查询跟指定项目下的所有标段有关系的公告
     *
     * @param noticeDTO projectId 项目ID
     * @return
     */
    @Override
    public Page<NoticeDTO> pageNoticeByProjectId(NoticeDTO noticeDTO) {
        Page<NoticeDTO> pages = noticeDTO.pages();
        baseMapper.pageNoticeByProjectId(pages, noticeDTO.getProjectId(), noticeDTO.getNoticeNature());
        for (NoticeDTO record : pages.getRecords()) {
            // 前端显示字段
            record.setDeployStatusName(Enums.of(DeployStatusEnum.class, record.getDeployStatus()).getShowName());
            record.setNoticeNatureName(Enums.of(NoticeNatureEnum.class, record.getNoticeNature()).getShowName());
        }
        return pages;
    }

    /**
     * 查询指定标段ID的公告分页信息
     *
     * @param noticeDTO
     * @return
     * @update zhaojk
     */
    @Override
    public Page<Notice> pageNoticeBySectionId(NoticeDTO noticeDTO) {
        Page<Notice> pages = noticeDTO.pages();
        return baseMapper.pageNoticeBySectionId(pages, noticeDTO.getSectionId(), noticeDTO.getNoticeNature());
    }

    /**
     * @param sectionId:
     * @Description: 查询标段最后一次发布的公告(除了控制价公告以外)
     * @Author: zhongdl
     * @Date:2021/12/21 16:36
     * @return: {@link Notice }
     **/
    @Override
    public Notice lastNoticeBySectionId(Long sectionId) {
        Notice notice = baseMapper.getLastNoticeBySectionId(sectionId);
        return notice != null ? notice : new Notice();
    }

    /**
     * 分页查询公告列表
     *
     * @param noticePortalDTO
     * @return
     */
    @Override
    public Page<NoticePortalDTO> pageNotice(NoticePortalDTO noticePortalDTO) {
        Page<NoticePortalDTO> noticePortalDTOPage = this.baseMapper.pageNotice(noticePortalDTO.pages(), noticePortalDTO);
        return noticePortalDTOPage;
    }

    /**
     * 查询公告详情
     *
     * @param noticePortalDTO
     * @return
     */
    @Override
    public NoticePortalDTO findNoticeByNoticeId(NoticePortalDTO noticePortalDTO) {
        NoticePortalDTO portalDTO = this.baseMapper.findNoticeByNoticeId(noticePortalDTO);
        return portalDTO;
    }

    @Override
    public List<NoticeDTO> listPushNotice() {
        return baseMapper.listPushNotice();
    }

    @Override
    public List<Notice> listNoticeBySectionId(NoticeListDTO noticeListDTO) {
        List<Notice> noticeList = this.baseMapper.listNoticeBySectionId(noticeListDTO);
        return noticeList;
    }

    /**
     * @Description: 根据项目ID查询公告
     *
     * @author yuezheng
     * @param projectId
     * @return java.util.List<com.hb.core.pojo.project.notice.entity.Notice>
     * @date 2022/6/30 17:27
     */
    @Override
    public List<Notice> findNoticeByProjectId(Long projectId) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getProjectId, projectId);
        List<Notice> noticeList = this.baseMapper.selectList(queryWrapper);
        return noticeList;
    }
}
